package net.hn.hnms.biz.safety.monitor.service;

import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.safety.monitor.model.ChartVo;
import net.hn.hnms.biz.safety.monitor.domain.AqjkNowSensorMonitorEntity;
import net.hn.hnms.biz.safety.monitor.repository.AqjkDictEntityRepository;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 智呈物联 pan'sx
 * 2022/10/10 10:48
 * 1.0 version
 */
@Service
@RequiredArgsConstructor
public class DustServiceImpl implements DustService {
    private final JdbcTemplate monitorJdbcTemplate;
    private final AqjkDictEntityRepository aqjkDictEntityRepository;

    @Override
    public List<AqjkNowSensorMonitorEntity> monitor(String sensorTypeName) {
        String sql = "select * from dwd_aqjk_now_sensormonitor where " +
                " data_time=(SELECT max(data_time) FROM \"dwd_aqjk_now_sensormonitor\") ";
        if (StringUtils.isNotBlank(sensorTypeName)) {
            sql += " and sensor_type_name='" + sensorTypeName + "'";
        }
        List<AqjkNowSensorMonitorEntity> list = monitorJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AqjkNowSensorMonitorEntity.class));
        if (!list.isEmpty()) {
            Map<String, String> point_status = aqjkDictEntityRepository.findByType("point_status").stream().collect(Collectors.toMap(s -> s.getPointLoactionName(), s -> s.getSimpleName()));
            list.forEach(s -> s.setStatus(point_status.get(s.getPointStatus())));
        }
        return list;
    }

    @Override
    public ChartVo alarmTrend(String type) {
        Calendar calendar = Calendar.getInstance();
        Date endTime = calendar.getTime();
        if ("月".equals(type)) {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        } else {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 7);
        }
        List<String> xDate = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        while (calendar.getTime().getTime() <= endTime.getTime()) {
            xDate.add(sdf.format(calendar.getTime()));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
        }
        String sql = "SELECT to_char(end_time,'yyyy-mm-dd') as day,sensor_type,count(1) as count " +
                "FROM \"dwd_aqjk_now_sensoralarm_count_stat\" " +
                "where   (sensor_type = '0014') and " +
                "end_time is not null  and max_value  is not  null " +
                "and end_time>'%s' and outlier_type='001' " +
                "GROUP BY day,sensor_type";
        String[] values = new String[xDate.size()];
        for (int i = 0; i < values.length; i++) {
            values[i] = "0";
        }
        List<Map<String, Object>> maps = monitorJdbcTemplate.queryForList(String.format(sql, xDate.get(0)));
//        List<String> strings = Arrays.asList(xdata);
        for (Map<String, Object> map : maps) {
            values[xDate.indexOf(map.get("day"))] = map.get("count") + "";
        }
        ChartVo alarmTrendVo = new ChartVo();
        alarmTrendVo.setXData(xDate);
        alarmTrendVo.setValeus(Arrays.asList(values));
        return alarmTrendVo;
    }

    @Override
    public ChartVo monitorTrend(String type, String pointCode) {
        Calendar calendar = Calendar.getInstance();
//        Date endTime = calendar.getTime();
        String format = "hh24";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        List<String> xDate = new ArrayList<>();
        String beginTime = "";
        if ("天".equals(type)) {
            SimpleDateFormat sdf1 = new SimpleDateFormat("HH");
            for (int i = 0; i < 24; i++) {
                xDate.add(sdf1.format(calendar.getTime()));
                if (i != 23) {
                    calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
                }
            }
        } else {
            format = "yyyy-mm-dd";
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < 7; i++) {
                xDate.add(sdf.format(calendar.getTime()));
                if (i != 6) {
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
                }
            }
        }
        Collections.reverse(xDate);
        String sql = "SELECT to_char(data_time,'%s') as sj,max(point_value) as value" +
                " FROM \"dwd_aqjk_his_sensormonitor\" where  data_time > to_timestamp('%s','yyyy-MM-dd %s')  and point_code ='%s' GROUP BY to_char(data_time,'%s')";

        List<Map<String, Object>> maps = monitorJdbcTemplate.queryForList(String.format(sql, format, sdf.format(calendar.getTime()),
                "hh24".equals(format) ? format : ""
                , pointCode, format));
        String[] values = new String[xDate.size()];
//        for (int i = 0; i < values.length; i++) {
//            values[i] = "0";
//        }
        for (Map<String, Object> map : maps) {
            values[xDate.indexOf(map.get("sj"))] = map.get("value") + "";
        }
        ChartVo alarmTrendVo = new ChartVo();
        alarmTrendVo.setXData(xDate);
        alarmTrendVo.setValeus(Arrays.asList(values));
        return alarmTrendVo;
    }
}
